﻿/// <reference path="../libs/jquery-1.8.2-vsdoc.js" />
var SCA = SCA || {};
SCA.UI = SCA.UI || {};
SCA.UI.DocumentTemplates = SCA.UI.DocumentTemplates || {};

SCA.UI.DocumentTemplates.Dialog = function (documentTemplatesTable) {
    this.documentTemplatesTable = documentTemplatesTable;

    this.Setup = function () {
        jQuery("#NewTemplateButton").button().click(function () {
            jQuery("#DocumentTemplateEditDialog").dialog({ title: "New Template", width: "800px" });
        });

        jQuery("#DocumentTemplateEditDialog").on("dialogclose", function (event, ui) { jQuery(".validity-modal-msg").hide(); });
        jQuery("#ButtonDocumentTemplateDialogPicture").click(function () { OpenAssetPicker("my title", GetDropDownSiteSettingSelectedText(), SelectPictureCallback) }); //this title must match with the one in SCA.AssetPicker.IsSobiensAssetPicker()
        jQuery("#ButtonDocumentTemplateDialogTemplate").click(function () { OpenAssetPicker("my title", GetDropDownSiteSettingSelectedText(), SelectTemplateCallback) });
    }

    this.New = function () {
        GetHiddenInputIndex().val("-1");
        GetFirstRadioButtonType().attr("checked", true);
        GetTextBoxTitle().val("");
        GetTextBoxDescription().val("");
        LoadSiteSettings(GetDropDownSiteSetting());
        GetTextBoxPicture().val("");
        GetTextBoxTemplate().val("");
    }

    this.Load = function (index) {

        if (SCA.AppConfiguration.DocumentTemplates == null)
            return;

        var documentTemplate = GetDocumentTemplate(index);

        GetRadioButtonType(documentTemplate).attr("checked", true);

        GetTextBoxTitle().val(documentTemplate.Title);

        GetTextBoxDescription().val(documentTemplate.Description);
        var dropDownSiteSettings = GetDropDownSiteSetting();
        LoadSiteSettings(dropDownSiteSettings);
        dropDownSiteSettings.val(documentTemplate.SiteSettingID);

        GetTextBoxPicture().val(documentTemplate.ImageURL);
        GetTextBoxTemplate().val(documentTemplate.TemplateURL);

        GetHiddenInputIndex().val(index);
    }

    var LoadSiteSettings = function (dropDownSiteSettings)
    {
        dropDownSiteSettings.html("");
        if ((SCA.AppConfiguration.SiteSettings != null) && (SCA.AppConfiguration.SiteSettings.length > 0))
        for (var i = 0; i < SCA.AppConfiguration.SiteSettings.length; i++) {
            var siteSetting = SCA.AppConfiguration.SiteSettings[i];
            jQuery("<option value='" + siteSetting.ID + "'>" + siteSetting.Url + "</option>").appendTo(dropDownSiteSettings); 
        }
    }

    this.Validate = function () {

        jQuery.validity.setup({ outputMode: "modal" });

        jQuery.validity.start();
        GetTextBoxTitle().require("Required.");
        GetTextBoxPicture().require("Required.");
        GetTextBoxTemplate().require("Required.");

        var result = jQuery.validity.end();

        return result.valid;
    }

    this.Close = function () {
        jQuery("#DocumentTemplateEditDialog").dialog("close");
    }

    this.Save = function () {

        var isValid = this.Validate();
        if (!isValid)
            return;

        var index = GetHiddenInputIndex().val();

        var isNewItem = false;
        if (index == -1) //it's a new doc template
        {
            index = SCA.AppConfiguration.DocumentTemplates.push({}) - 1;
            isNewItem = true;
        }

        var docTemplate = GetDocumentTemplate(index);
        docTemplate.SiteSettingID = GetDropDownSiteSetting().val();
        docTemplate.ApplicationType = GetSelectedRadioButtonType().val();
        docTemplate.Title = GetTextBoxTitle().val();
        docTemplate.Description = GetTextBoxDescription().val();
        //docTemplate.ImageID = //Leave the ImageId as it is, we need that in the client side. If it is a new Template leave as null.
        docTemplate.ImageURL = GetTextBoxPicture().val();
        docTemplate.TemplateURL = GetTextBoxTemplate().val();

        if (isNewItem) {
            docTemplate.ID = SP.Guid.newGuid().toString();
            this.documentTemplatesTable.AddRow(index);
        }
        else {
            this.documentTemplatesTable.RefreshRow(index);
        }

        this.Close();
    }


    var OpenAssetPicker = function (title, siteCollectionUrl, callback) {
        var dialog = new SCA.SPDialog(title, siteCollectionUrl, callback);
        dialog.OpenAssetPicker();
    }

    var SelectPictureCallback = function(result, target)
    {
        if (result === 1)
        {
            GetTextBoxPicture().val(target);
        }
    }

    var SelectTemplateCallback = function(result, target)
    {
        if (result == 1)
        {
            GetTextBoxTemplate().val(target);
        }
    }
    

    var GetDocumentTemplate = function (index) {
        return SCA.AppConfiguration.DocumentTemplates[index];
    }

    var GetRadioButtonType = function (documentTemplate) {
        var idRadioButtonType = "#RadioButtonDocumentTemplateDialogType" + documentTemplate.ApplicationType;
        return radioButtonType = jQuery(idRadioButtonType);
    }

    var GetFirstRadioButtonType = function()
    {
        return jQuery("input[name=DocumentTemplateType]:first");
    }

    var GetSelectedRadioButtonType = function()
    {
        return jQuery("input[name=DocumentTemplateType]:checked");
    }

    var GetTextBoxTitle = function () {
        return jQuery("#TextBoxDocumentTemplateDialogTitle");
    }

    var GetTextBoxDescription = function () {
        return jQuery("#TextBoxDocumentTemplateDialogDescription");
    }

    var GetDropDownSiteSetting = function () {
        return jQuery("#DropDownDocumentTemplateDialogSiteSetting");
    }

    var GetDropDownSiteSettingSelectedText = function () {
        return jQuery("#DropDownDocumentTemplateDialogSiteSetting option:selected").text();
    }


    var GetTextBoxPicture = function () {
        return jQuery("#TextboxDocumentTemplateDialogPicture");
    }

    var GetTextBoxTemplate = function () {
        return jQuery("#TextboxDocumentTemplateDialogTemplate");
    }

    var GetHiddenInputIndex = function () {
        return jQuery("#HiddenFieldDocumentTemplateDialogCurrentIndex");
    }

    this.Setup();
}